package com.ht.safe.entity;

import com.ht.safe.entity.ext.Action;
import com.ht.safe.entity.ext.AuthenticationType;
import org.hibernate.annotations.Type;
import org.springframework.data.jpa.domain.AbstractPersistable;

import javax.persistence.*;
import java.util.Date;
import java.util.List;

/**
 * 通行人员授权信息
 * todo: 认证信息过期后的处理方式
 */
@Entity
@Table(name="T_Credential")
public class Credential extends AbstractPersistable<Integer> {


    @ManyToOne(optional = false)
    @JoinColumn(name = "member_id", nullable = false)
    private Member member;

    @Enumerated(EnumType.STRING)
    @Column(name = "authentication_type")
    private AuthenticationType authenticationType;

    /**
     * 有效期起始时间, 精确到分钟
     */
    @Column(name = "active_start")
    @Temporal(TemporalType.TIMESTAMP)
    private Date activeStart;

    /**
     * 有效期结束时间
     */
    @Column(name = "active_end")
    @Temporal(TemporalType.TIMESTAMP)
    private Date activeEnd;

    /**
     * PIN码, 授权模板中没有该属性
     */
    @Column(name = "pin_code")
    private String pinCode;

    /**
     * pin码长度
     */
    @Column(name = "pin_code_len")
    private Integer pinCodeLen;

    /**
     * 胁迫码, 授权模板中没有该属性
     */
    @Column(name = "duress_code")
    private String duressCode;

    /**
     * 胁迫码长度
     */
    @Column(name = "duress_code_len")
    private Integer duressCodeLen;

    /**
     * 胁迫等级(0-99)
     */
    @Column(name = "duress_authority")
    private Integer duressAuthority;

    /**
     * 时间规则特权
     */
    @Column(name = "time_rule")
    @Type(type = "org.hibernate.type.NumericBooleanType")
    private Boolean timeRule;

    /**
     * 线路规则特权(潜回)
     */
    @Column(name = "line_rule")
    @Type(type = "org.hibernate.type.NumericBooleanType")
    private Boolean lineRule;

    /**
     * 多人规则特权
     */
    @Column(name = "ab_rule")
    @Type(type = "org.hibernate.type.NumericBooleanType")
    private Boolean abRule;

    /**
     * 延长开门时间,单位为秒，允许为0或空值
     */
    @Column(name = "access_time_extension")
    private Integer accessTimeExtension;

    /**
     * 认证时需要操作器执行的操作, 如果是空则忽略该项
     */
    @Enumerated(EnumType.STRING)
    private Action action;

    /**
     * 证章模板
     */
    @Column(name = "badge_template")
    private BadgeTemplate badgeTemplate;

    /**
     * 卡信息
     */
    @OneToOne(cascade=CascadeType.ALL, fetch = FetchType.EAGER)
    @MapsId
    //@JoinColumn(name="card_id", unique = true)
    private Card card;

    /**
     * 允许通过的门组列表
     * TODO: 感觉直接在门组中设置允许通过的人组更简单一些
     */
/*    @ManyToMany
    @JoinTable(name="T_Member_Door_Groups",
            joinColumns=
            @JoinColumn(name="member_ID", referencedColumnName="ID"),
            inverseJoinColumns=
            @JoinColumn(name="door_group_ID", referencedColumnName="ID")
    )
    private List<DoorGroup> doorGroups;*/

    /**
     * 允许通过的区域列表
     */
//    @ManyToMany
//    @JoinTable(name="T_Member_Area",
//            joinColumns=
//            @JoinColumn(name="member_ID", referencedColumnName="ID"),
//            inverseJoinColumns=
//            @JoinColumn(name="area_ID", referencedColumnName="ID")
//    )
//    private List<Area> areas;
    @ElementCollection(fetch=FetchType.EAGER)
    private List<String> areas;

	public Credential() {
	}

	public Credential(Integer id) {
		this.setId(id);
	}

    public Date getActiveStart() {
        return activeStart;
    }

    public void setActiveStart(Date activeStart) {
        this.activeStart = activeStart;
    }

    public Date getActiveEnd() {
        return activeEnd;
    }

    public void setActiveEnd(Date activeEnd) {
        this.activeEnd = activeEnd;
    }

    public String getPinCode() {
        return pinCode;
    }

    public void setPinCode(String pinCode) {
        this.pinCode = pinCode;
    }

    public Integer getPinCodeLen() {
        return pinCodeLen;
    }

    public void setPinCodeLen(Integer pinCodeLen) {
        this.pinCodeLen = pinCodeLen;
    }

    public String getDuressCode() {
        return duressCode;
    }

    public void setDuressCode(String duressCode) {
        this.duressCode = duressCode;
    }

    public Integer getDuressCodeLen() {
        return duressCodeLen;
    }

    public void setDuressCodeLen(Integer duressCodeLen) {
        this.duressCodeLen = duressCodeLen;
    }

    public Integer getDuressAuthority() {
        return duressAuthority;
    }

    public void setDuressAuthority(Integer duressAuthority) {
        this.duressAuthority = duressAuthority;
    }

    public Boolean getTimeRule() {
        return timeRule;
    }

    public void setTimeRule(Boolean timeRule) {
        this.timeRule = timeRule;
    }

    public Boolean getLineRule() {
        return lineRule;
    }

    public void setLineRule(Boolean lineRule) {
        this.lineRule = lineRule;
    }

    public Boolean getAbRule() {
        return abRule;
    }

    public void setAbRule(Boolean abRule) {
        this.abRule = abRule;
    }

    public Integer getAccessTimeExtension() {
        return accessTimeExtension;
    }

    public void setAccessTimeExtension(Integer accessTimeExtension) {
        this.accessTimeExtension = accessTimeExtension;
    }

    public Action getAction() {
        return action;
    }

    public void setAction(Action action) {
        this.action = action;
    }

    public BadgeTemplate getBadgeTemplate() {
        return badgeTemplate;
    }

    public void setBadgeTemplate(BadgeTemplate badgeTemplate) {
        this.badgeTemplate = badgeTemplate;
    }

    public Card getCard() {
        return card;
    }

    public void setCard(Card card) {
        this.card = card;
    }

    public List<String> getAreas() {
        return areas;
    }

    public void setAreas(List<String> areas) {
        this.areas = areas;
    }

    public Member getMember() {
        return member;
    }

    public void setMember(Member member) {
        this.member = member;
    }
}
